#include "cfdl_login.h"

cfdl_login::cfdl_login(QObject *parent)
    : QObject{parent}
{
    connect(this, &cfdl_login::signalLog, UsrCommImpl::Instance(), &UsrCommImpl::CommSlotLog);

    memset(devID, 0, sizeof(devID));
}

cfdl_login* cfdl_login::Instance()
{
    static cfdl_login ins;

    return &ins;
}

void cfdl_login::cfdl_msg_cb(cfdl_message_t* msg)
{
    if(msg == nullptr) return;

    if(CF_ISSELF(msg->src)) {
        emit signalLog(UsrCommImpl::logDateTime, nullptr, "local heart");
        return;
    }

    if(devNum < DEV_NUM) {
        int i = 0;
        for(; i < DEV_NUM; i++) {
            if(devID[i].vid == msg->src.vid && devID[i].pid == msg->src.pid && devID[i].chid == msg->src.chid)
                break;
        }

        if(i >= DEV_NUM) {
            devID[devNum].vid  = msg->src.vid;
            devID[devNum].pid  = msg->src.pid;
            devID[devNum].chid = msg->src.chid;
            devNum++;

            QString tmp = QString("Dev login, VID:%1, PID:%2, CHID:%3")
                              .arg(msg->src.vid, 4, 16, QChar('0')).arg(msg->src.pid, 4, 16, QChar('0')).arg(msg->src.chid, 4, 16, QChar('0'));
            emit signalLog(UsrCommImpl::logDateTime, ">>>", tmp);
            emit signalDevLogin(msg->src.vid, msg->src.pid, msg->src.chid);
        }
    }
}

void cfdl_login::cfdl_msg_err(cfdl_message_t* msg)
{
    if(msg == nullptr) return;
}
